package com.yundasys.lab12java.proxy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Title: CglibProxy
 * @Description:
 * @author: jiangta
 * @date: 2023/4/27 2:34 下午
 * @Version: 1.0
 */
@Slf4j
public class CglibProxy implements MethodInterceptor {

    public Object newInstall(Object object) {
        return Enhancer.create(object.getClass(),this);
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("我被CglibProxy代理了");
        return methodProxy.invokeSuper(o,  objects);
    }

    public static void main(String[] args) {
        CglibProxy cglibProxy = new CglibProxy();
        UserApi userApi = (UserApi) cglibProxy.newInstall(new UserApi());
        String invoke = userApi.queryUserInfo();
        log.info("测试结果：{}", invoke);
    }
}